package gui.commons;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;

public class CommonLayoutOperations {
	private GridBagConstraints gc;
	private Insets lblInset, tfInset, btnInset;

	public CommonLayoutOperations(Insets lblInset, Insets tfInset, Insets btnInset) {
		/*
		 * layout variables
		 */
		gc = new GridBagConstraints();
		this.lblInset = (lblInset == null ? new Insets(0, 0, 0, 1) : lblInset);
		this.tfInset = (tfInset == null ? new Insets(0, 1, 0, 1) : tfInset);
		this.btnInset = (btnInset == null ? new Insets(1, 1, 1, 1) : btnInset);
	}

	public void addJPanel(JPanel sourcePanel, JPanel jp, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		jp.setMinimumSize(new Dimension(
				(int) (jp.getPreferredSize().width * 1.2), (int) (jp
						.getPreferredSize().height * 1.2)));
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 1;
		gc.ipady = 10;
		// uso el mismo inset que los botones
		gc.insets = btnInset;
		sourcePanel.add(jp, gc);
	}
	
//	public void addVJPanel(JPanel sourcePanel, JPanel jp, int x, int y) {
//		gc.anchor = GridBagConstraints.LINE_START;
//		jp.setMinimumSize(new Dimension(
//				(int) (jp.getPreferredSize().width * 1.2), (int) (jp
//						.getPreferredSize().height * 1.2)));
//		gc.gridx = x;
//		gc.gridy = y;
//		gc.ipadx = 1;
//		gc.ipady = 10;
//		// uso el mismo inset que los botones
//		gc.insets = btnInset;
//		sourcePanel.add(jp, gc);
//	}

	public void addJLabel(JPanel sourcePanel, String lbl, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_END;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 5;
		gc.ipady = 5;
		gc.insets = lblInset;
		sourcePanel.add(new JLabel(lbl), gc);
	}

	public void addJLabel(JDialog source, String lbl, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_END;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 5;
		gc.ipady = 5;
		gc.insets = lblInset;
		source.add(new JLabel(lbl), gc);
	}

	public void addLabel(JPanel sourcePanel, JLabel lbl, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_END;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 5;
		gc.ipady = 5;
		gc.insets = lblInset;
		sourcePanel.add(lbl, gc);
	}

	public void addJCheckBox(JPanel sourcePanel, JCheckBox ch, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_END;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 5;
		gc.insets = tfInset;
		sourcePanel.add(ch, gc);
	}

	public void addTextField(JPanel sourcePanel, JTextField t, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		t.setMinimumSize(t.getPreferredSize());
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		gc.insets = tfInset;
		sourcePanel.add(t, gc);
	}

	public void addTextField(JDialog source, JTextField t, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		t.setMinimumSize(t.getPreferredSize());
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		gc.insets = tfInset;
		source.add(t, gc);
	}

	public void addJPasswordField(JDialog source, JPasswordField pf, int x,
			int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		pf.setMinimumSize(pf.getPreferredSize());
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		gc.insets = tfInset;
		source.add(pf, gc);
	}

	public void addJPasswordField(JPanel source, JPasswordField pf, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		pf.setMinimumSize(pf.getPreferredSize());
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		gc.insets = tfInset;
		source.add(pf, gc);
	}

	public void addJButton(JPanel sourcePanel, JButton b, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 2;
		gc.ipady = 1;
		gc.insets = btnInset;
		sourcePanel.add(b, gc);
	}

	public void addJButton(JDialog source, JButton b, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 2;
		gc.ipady = 1;
		gc.insets = btnInset;
		source.add(b, gc);
	}

	public void addJSpinner(JDialog source, JSpinner sp, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 2;
		gc.ipady = 1;
		gc.insets = btnInset;
		source.add(sp, gc);
	}

	public void addJSpinner(JPanel source, JSpinner sp, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 2;
		gc.ipady = 1;
		gc.insets = btnInset;
		source.add(sp, gc);
	}

	public void addJComboBox(JPanel sourcePanel, JComboBox jcb, boolean b,
			int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		jcb.setMinimumSize(new Dimension(
				(int) (jcb.getPreferredSize().width * .175), jcb
						.getPreferredSize().height));
		jcb.setBorder(BorderFactory.createEtchedBorder());
		jcb.setSelectedIndex(0);
		jcb.setEnabled(b);
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		gc.ipady = 5;
		// using same inset as textfields
		gc.insets = tfInset;
		sourcePanel.add(jcb, gc);
	}

	public void addJRadioButton(JPanel sourcePanel, JRadioButton rb, int x,
			int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 1;
		gc.ipady = 0;
		gc.weighty = 0.01;
		// I'm using same insets as the textfields'
		gc.insets = tfInset;
		sourcePanel.add(rb, gc);
	}

	public void addJList(JPanel sourcePanel, JList l, int x, int y) {
		gc.anchor = GridBagConstraints.LINE_START;
		l.setMinimumSize(l.getPreferredSize());
		l.setBorder(BorderFactory.createEtchedBorder());
		l.setSelectedIndex(0);
		gc.gridx = x;
		gc.gridy = y;
		gc.ipadx = 110;
		// using same inset as textfields
		gc.insets = tfInset;
		sourcePanel.add(l, gc);
	}

}
